import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Home',
        redirect: '/home', // 重定向:重新指向其它path,会改变网址
    }, {
        path: '/home',
        name: 'Home',
        component: Home,
        redirect: '/home/homePage',
        children: [{
            path: 'homePage',
            name: 'HomePage',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/HomePage.vue'),
            meta: {
                title: '首页',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }, {
            path: 'findHouse',
            name: 'FindHouse',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/FindHouse.vue'),
            meta: {
                title: '找房',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }, {
            path: 'release',
            name: 'Release',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/Release.vue'),
            meta: {
                title: '发布',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }, {
            path: 'news',
            name: 'News',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/News.vue'),
            meta: {
                title: '消息',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }, {
            path: 'mypage',
            name: 'MyPage',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/MyPage.vue'),
            meta: {
                title: '我的',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }]
    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/Login.vue')
    }, {
        path: '/my',
        name: 'My',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/My.vue')
    }, {
        path: '/detail',
        name: 'Detail',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/Detail.vue'),
            meta:{
                title:'详情'
            }
    }, {
        path: '*',
        name: 'NotFound',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/NotFound.vue')
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    document.title = to.meta.title
    if (to.name == 'MyPage') {
        const isLogin = localStorage.getItem('isLogin')
        if (isLogin == 'true') {
            console.log('已登录');
        } else {
            console.log('未登录');
            next('/login')
        }
    }
    next()
})
export default router